package com.ruoyi.checkingin.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.checkingin.controller.EChecksController;
import com.ruoyi.checkingin.service.IECheckTimeService;
import com.ruoyi.common.core.domain.e_checkingin.ECheckTime;
import com.ruoyi.common.core.domain.e_checkingin.EChecks;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.checkingin.mapper.ERpRecordMapper;
import com.ruoyi.common.core.domain.e_checkingin.ERpRecord;
import com.ruoyi.checkingin.service.IERpRecordService;

/**
 * 奖惩记录Service业务层处理
 * 
 * @author JYW
 * @date 2024-01-30
 */
@Service
public class ERpRecordServiceImpl implements IERpRecordService 
{
    @Autowired
    private ERpRecordMapper eRpRecordMapper;

    @Autowired
    private IECheckTimeService checkTimeService;

    @Autowired
    private EChecksController eChecksController;

    /**
     * 查询奖惩记录
     * 
     * @param id 奖惩记录主键
     * @return 奖惩记录
     */
    @Override
    public ERpRecord selectERpRecordById(Long id)
    {
        return eRpRecordMapper.selectERpRecordById(id);
    }

    /**
     * 查询奖惩记录列表
     * 
     * @param eRpRecord 奖惩记录
     * @return 奖惩记录
     */
    @Override
    public List<ERpRecord> selectERpRecordList(ERpRecord eRpRecord)
    {
        return eRpRecordMapper.selectERpRecordList(eRpRecord);
    }

    /**
     * 根据时间（每月）查询奖惩记录列表
     * @param userId
     * @param date
     * @return
     */
    @Override
    public List<ERpRecord> selectERpRecordListByTime(Long userId,String date){
        return eRpRecordMapper.selectERpRecordListByTime(userId,date);
    };


    /**
     * 新增奖惩记录
     * 
     * @param eRpRecord 奖惩记录
     * @return 结果
     */
    @Override
    public int insertERpRecord(ERpRecord eRpRecord)
    {
        eRpRecord.setCreateTime(DateUtils.getNowDate());
        return eRpRecordMapper.insertERpRecord(eRpRecord);
    }

    /**
     * 修改奖惩记录
     * 
     * @param eRpRecord 奖惩记录
     * @return 结果
     */
    @Override
    public int updateERpRecord(ERpRecord eRpRecord)
    {
        eRpRecord.setUpdateTime(DateUtils.getNowDate());
        return eRpRecordMapper.updateERpRecord(eRpRecord);
    }

    /**
     * 批量删除奖惩记录
     * 
     * @param ids 需要删除的奖惩记录主键
     * @return 结果
     */
    @Override
    public int deleteERpRecordByIds(Long[] ids)
    {
        return eRpRecordMapper.deleteERpRecordByIds(ids);
    }

    /**
     * 删除奖惩记录信息
     * 
     * @param id 奖惩记录主键
     * @return 结果
     */
    @Override
    public int deleteERpRecordById(Long id)
    {
        return eRpRecordMapper.deleteERpRecordById(id);
    }

    @Override
    public int autoSentence() {
        //1.初始化变量
        ERpRecord eRpRecord = new ERpRecord();
        Long userId = SecurityUtils.getUserId();
        eRpRecord.setuId(userId);
        ECheckTime eCheckTime = new ECheckTime();
        eCheckTime.setuId(userId);
        //2.获取所有考勤规则
        List<EChecks> eChecks = (List<EChecks>) eChecksController.list(new EChecks()).getRows();
        //3.获取所有当前账号用户的考勤记录
        List<ECheckTime> eCheckTimes = checkTimeService.selectECheckTimeList(eCheckTime);
            //3.1 获取已经处理的考勤记录长度
        int count =0;
            eCheckTime.setIsDispose(1);
            count = checkTimeService.selectECheckTimeList(eCheckTime).size();

        //4.判断，当ture时，证明今天之前有未处理的考勤记录
                if ((eCheckTimes.size()-count)>=1){
                    //处理之前未处理的考勤记录
                     this.handleBeforeCheckRecord(eCheckTimes,eChecks,eRpRecord);
        }
        return 1;
    }

    /*判断，当ture时，证明今天之前有未处理的考勤记录*/
    public void handleBeforeCheckRecord(List<ECheckTime> eCheckTimes,List<EChecks> eChecks,ERpRecord eRpRecord){
        //4.1获取所有未做处理的考勤记录，除了最后一条
        eCheckTimes = eCheckTimes.stream().filter(eCheckTime1 -> eCheckTime1.getIsDispose() == null).collect(Collectors.toList());
        eCheckTimes.remove(eCheckTimes.size()-1);
        //4.2循环处理未处理的记录
        eCheckTimes.forEach(eCheckTime1 -> {
            //5.筛选出异常的考勤状态是否异常,异常则新增
            List<EChecks> collect = eChecks.stream().filter(eChecks1 -> eChecks1.getStage().equals(eCheckTime1.getSignInStatus())  && eChecks1.getRemark() != null).collect(Collectors.toList());
            List<EChecks> collect2 = eChecks.stream().filter(eChecks1 -> eChecks1.getStage().equals(eCheckTime1.getSignOutStatus())  && eChecks1.getRemark() != null).collect(Collectors.toList());
            if (!collect.isEmpty()){
                eRpRecord.setRpId(eCheckTime1.getSignInStatus());
                insertERpRecord(eRpRecord);
            }
            if (!collect2.isEmpty()){
                eRpRecord.setRpId(eCheckTime1.getSignOutStatus());
                insertERpRecord(eRpRecord);
            }
            //6.将改考勤记录标记为已处理
            eCheckTime1.setIsDispose(1);
            checkTimeService.updateECheckTime(eCheckTime1);

        });
    }
}
